<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Site;

class SiteController extends Controller
{
    protected $message = [
        'name.required' => '公司名称必须',
        'name.max' => '公司名称不能超过50个字符',
        'company_id.required' => '请选择项目',
        'is_default.unique' => '默认项目已经设置',
    ];
    
    /**
     * 站点列表
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $perPage = $request->input('pageSize',15);
        $name = $request->input('name', '');
        $query = Site::query();
        if(!empty($name)) {
            $query->where('name', 'like', $name.'%');
        }
        $cid = $request->input('cid', '');
        if(!empty($cid)) {
            $query->where('company_id', $cid);
        }
        $created = $request->input('created_at', []);
        if(!empty($created)) {
            $query->whereBetween('created_at', $created);
        }
        
        $query->with([
            'project' => function($query) {
                $query->select(['Co_ID', 'Co_Name']);
            },
        ]);

        $list = $query->paginate($perPage);
        return $list;
    }



    /**
     * 保存站点
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = $request->input();
        $v = $this->validate2($request, [
            'name'=>'required|max:50',
            'company_id'=>'required|integer|exists:t_companycnfo,Co_ID'
        ], $this->message);

        $v->sometimes('is_default', 'unique:sites,is_default', function($input){
            return $input->is_default;
        });
        
        $v->validate();
        $ret = Site::create($data);
        if(!$ret){
            $this->error('添加失败');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        
    }



    /**
     * 更新站点
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $data = $request->input();
        $v = $this->validate2($request, [
            'name'=>'required|max:50',
            'company_id'=>'required|integer|exists:t_companycnfo,Co_ID'
        ], $this->message);

        $v->sometimes('is_default', 'unique:sites,is_default,'.$id.',id,company_id,'.$cid, function($input){
            return $input->is_default;
        });
        $v->validate();
        
        $site = Site::find($id);
        $ret = $site->update($data);
        if(!$ret){
            $this->error('保存失败');
        }
    }

    /**
     * 删除站点
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $site = Site::findOrFail($id);
        if(!$site->delete()){
            $this->error('删除失败');
        }
    }
    
    /**
     * 创建管线
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function storePipeline(Request $request)
    {
        $data = $request->input();
        $query = \App\Models\WaterLine::where('company_id', array_get($data, 'company_id'))
                ->where('site_id', array_get($data, 'site_id')+0);
        $info =  $query->first();
        $type = $request->input('type', 'content');
        $pipeData = array_only($data, ['site_id', 'company_id']);
        if($info) {
            $pipeData['site_id'] = array_get($data, 'site_id')+0;
            $pipeData[$type] = json_encode(array_get($data, 'content'));
            $ret = $query->update($pipeData);
        }else{
            $pipeData[$type] = array_get($data, 'content');
            $ret = \App\Models\WaterLine::create($pipeData);
        }
        
        if(!$ret){
            $this->error('保存失败');
        }
    }
    
    /**
     * 更新管线
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function updatePipeline(Request $request, $id)
    {
        $data = $data = $request->input();
        $info = \App\Models\WaterLine::findOrFail($id);
        $ret = $info->update($data);
        if(!$ret){
            $this->error('更新失败');
        }
    }
    /**
     * 获取管线信息
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function showPipeline(Request $request)
    {
        $data = $request->input();
        $query = \App\Models\WaterLine::where('company_id', array_get($data, 'company_id', 0))
                ->where('site_id', array_get($data, 'site_id', 0)+0);
        $info =  $query->get()->first();
        return ['data'=>$info];
    }
    
    /**
     * 获取所有管线信息
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function showAllPipeline(Request $request)
    {
        $data = $request->input();
        $query = \App\Models\WaterLine::where('company_id', array_get($data, 'company_id', 0));
        $info =  $query->get();

        return ['data'=>$info];
    }

}
